R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

setwd("~/crimes new")

library(readr)
crime <- read_csv("data/crime_and_incarceration_by_state.csv")
## Parsed with column specification:
## cols(
##   jurisdiction = col_character(),
##   includes_jails = col_logical(),
##   year = col_double(),
##   prisoner_count = col_double(),
##   crime_reporting_change = col_logical(),
##   crimes_estimated = col_logical(),
##   state_population = col_double(),
##   violent_crime_total = col_double(),
##   murder_manslaughter = col_double(),
##   rape_legacy = col_double(),
##   rape_revised = col_double(),
##   robbery = col_double(),
##   agg_assault = col_double(),
##   property_crime_total = col_double(),
##   burglary = col_double(),
##   larceny = col_double(),
##   vehicle_theft = col_double()
## )
prison <- read_csv("data/prison_custody_by_state.csv")
## Parsed with column specification:
## cols(
##   jurisdiction = col_character(),
##   includes_jails = col_double(),
##   `2001` = col_number(),
##   `2002` = col_number(),
##   `2003` = col_number(),
##   `2004` = col_number(),
##   `2005` = col_number(),
##   `2006` = col_number(),
##   `2007` = col_number(),
##   `2008` = col_number(),
##   `2009` = col_number(),
##   `2010` = col_number(),
##   `2011` = col_number(),
##   `2012` = col_number(),
##   `2013` = col_number(),
##   `2014` = col_number(),
##   `2015` = col_number(),
##   `2016` = col_number()
## )
ucr <- read_csv("data/ucr_by_state.csv")
## Warning: Missing column names filled in: 'X16' [16], 'X17' [17],
## 'X18' [18], 'X19' [19], 'X20' [20], 'X21' [21]
## Parsed with column specification:
## cols(
##   .default = col_number(),
##   jurisdiction = col_character(),
##   year = col_double(),
##   crime_reporting_change = col_double(),
##   crimes_estimated = col_double(),
##   X16 = col_logical(),
##   X17 = col_logical(),
##   X18 = col_logical(),
##   X19 = col_logical(),
##   X20 = col_logical(),
##   X21 = col_logical()
## )
## See spec(...) for full column specifications.
library(magick)
## Linking to ImageMagick 6.9.9.14
## Enabled features: cairo, freetype, fftw, ghostscript, lcms, pango, rsvg, webp
## Disabled features: fontconfig, x11
library(ggplot2)

ucr <- ucr[!is.na(ucr$jurisdiction),]
ucr$year <- as.integer(ucr$year)

p <- ggplot(ucr, aes(x = violent_crime_total, 
                     y = state_population, 
                     colour = as.factor(jurisdiction))) +
  geom_point(show.legend = FALSE, alpha = 0.7) +
  scale_color_viridis_d() +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(x = "Violent crimes total", y = "State population")

# install.packages("gganimate")
library("gganimate")

p + transition_time(year) + labs(title = “Year: {frame_time}”, range=c(2001L,2017L))

library(tmap)
library(spData)
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
us_states2163 = st_transform(us_states, 2163)
us_states_range = st_bbox(us_states2163)[4] - st_bbox(us_states2163)[2]

us_states_map = tm_shape(us_states2163) +
  tm_polygons() + 
  tm_layout(frame = FALSE)
us_states_map

tm_shape(us_states2163) +
  tm_polygons("total_pop_10")

# install.packages("gganimate")
library("gganimate")

bubbles <- p + transition_time(year) +
  labs(title = "Year: {frame_time}", range=c(2001L,2017L))
anim_save("bubbles.gif", bubbles)
bubbles